package com.fotoloka.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;


@SuppressWarnings("serial")
public class ImagePanel extends JPanel implements MouseListener 
{
	private Image img;
	
	private boolean center = true;
	
	 private int margemSuperior = 10; 
	
	private int margemLateral = 10;
	
	private boolean redimensionar = false;
	
	private boolean ajustarAJanela = true;
	
	public boolean isAjustarAJanela() {
		return ajustarAJanela;
	}

	public void setAjustarAJanela(boolean ajustarAJanela) {
		this.ajustarAJanela = ajustarAJanela;
	}

	public ImagePanel() {}
	
	public ImagePanel(Image img, boolean redimensionar, boolean ajustarAJanela) {
		this(img);
		this.center = true;
		this.setBackground(new Color(255,0,0));
		//this.setSize(width,height);
		this.redimensionar = redimensionar;
		this.ajustarAJanela = ajustarAJanela;
	}
	
	public ImagePanel(Image img)
	{
		this.img = img;
		this.setBackground(Color.white);
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{	
		super.paintComponent(g);
		
		if (img == null)
			return;
		
		if (center)
		{
			
			if(redimensionar) {
				img = img.getScaledInstance(this.getWidth()-margemLateral*2, this.getHeight()-margemSuperior*2,Image.SCALE_AREA_AVERAGING);
			}
			
			Dimension size = this.getSize();
			int width = img.getWidth(this);
			int height = img.getHeight(this);
			
			g.drawImage(img, size.width / 2 - width / 2, size.height / 2 - height / 2, this);
		}
		else
		{	
			g.drawImage(img, 0, 0 ,this);
		}
	}
	
	public void setImage(Image img)
	{
		Dimension size = this.getSize();
		if(ajustarAJanela) {
			this.img = img.getScaledInstance(size.width, size.height, Image.SCALE_AREA_AVERAGING);
		} else {
			this.img = img;
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		System.out.println("mouse entered");
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		System.out.println("mouse exited");
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("mouse pressed");
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		System.out.println("mouse released");
		
	}

	public Image getImg() {
		return img;
	}

	public boolean isRedimensionar() {
		return redimensionar;
	}

	public void setRedimensionar(boolean redimensionar) {
		this.redimensionar = redimensionar;
	}
}
